import type { ForgeConfig, } from '@electron-forge/shared-types';
import { MakerSquirrel } from '@electron-forge/maker-squirrel';
import { MakerZIP } from '@electron-forge/maker-zip';
// import { MakerDeb } from '@electron-forge/maker-deb';
// import { MakerRpm } from '@electron-forge/maker-rpm';
import { AutoUnpackNativesPlugin } from '@electron-forge/plugin-auto-unpack-natives';
import { WebpackPlugin } from '@electron-forge/plugin-webpack';
import { FusesPlugin } from '@electron-forge/plugin-fuses';
import { FuseV1Options, FuseVersion } from '@electron/fuses';
import { ElectronDownloadCacheMode } from '@electron/get';
import { mainConfig } from './webpack.main.config';
import { rendererConfig } from './webpack.renderer.config';

import fs from 'fs-extra';

const config: ForgeConfig = {
  packagerConfig: {
    asar: true,
    download: {
      mirrorOptions: {
        //mirror: "file:///D:/jwork/get-file-info2/electron-offline-packages/",
        mirror: "https://npmmirror.com/mirrors/electron/",
      },
      //cacheRoot: "C:/Users/carota.user/AppData/Local/electron/Cache/",
      //cacheMode: ElectronDownloadCacheMode.ReadOnly,
      //   // },
      //   // download: {
      //   //     cacheMode: ElectronDownloadCacheMode.ReadWrite,
    },
    afterCopyExtraResources: [async (buildPath, electronVersion, platform, arch, callback) => {
      await fs.copy("./resources/analyzers.json", buildPath + "/resources/analyzers.json");
      //await fs.copy("./resources/logkit", buildPath + "/resources/logkit");//目录
      callback();
    }],
    extraResource: [
      "./dummy.txt"//没作用，只是为了触发 afterCopyExtraResources
    ]
  },
  rebuildConfig: {},
  makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin'])],//, new MakerRpm({}), new MakerDeb({})
  plugins: [
    new AutoUnpackNativesPlugin({}),
    new WebpackPlugin({
      mainConfig,
      renderer: {
        config: rendererConfig,
        entryPoints: [
          {
            html: './src/index.html',
            js: './src/renderer.tsx',
            name: 'main_window',
            preload: {
              js: './src/preload.ts',
            },
          },
        ],
      },
    }),
    // Fuses are used to enable/disable various Electron functionality
    // at package time, before code signing the application
    new FusesPlugin({
      version: FuseVersion.V1,
      [FuseV1Options.RunAsNode]: false,
      [FuseV1Options.EnableCookieEncryption]: true,
      [FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
      [FuseV1Options.EnableNodeCliInspectArguments]: false,
      [FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
      [FuseV1Options.OnlyLoadAppFromAsar]: true,
    }),
  ],
};

export default config;
